package com.dfbz.netctoss.controller;

import com.dfbz.netctoss.Result;
import com.dfbz.netctoss.entity.AdminInfo;
import com.dfbz.netctoss.service.AccountService;
import com.dfbz.netctoss.service.AdminInfoService;
import com.dfbz.netctoss.utils.EncryptUtils;
import com.dfbz.netctoss.utils.ShiroUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletSecurityElement;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminInfoController {

    @Autowired
    AdminInfoService service;

    @RequestMapping("toList")
    public String toIndex(){
        return "admin/admin_list";
    }

    @RequestMapping("selectPage/{pageNum}/{pageSize}")
    @ResponseBody
    public Result selectPage(@PathVariable int pageNum, @PathVariable int pageSize){
        return new Result(true, "查询成功", service.selectPage(pageNum, pageSize));
    }

    @RequestMapping("logout")
    public String logout(HttpSession session){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "redirect:/login.html";
    }

    @RequestMapping("toDetail")
    @ResponseBody
    public Result toDetail(){
        Subject subject = SecurityUtils.getSubject();
        String principal = (String) subject.getPrincipal();
        AdminInfo adminInfo = service.findRolesByAdminCode(principal);
        return new Result(true, "查询成功", adminInfo);
    }

    @RequestMapping("doUpdateDetail")
    @ResponseBody
    public Result doUpdateDetail(@RequestBody AdminInfo adminInfo){
        int result = service.updateDetail(adminInfo);
//        ShiroUtils.setAdmin(adminInfo);
        return new Result(true, "更新成功", result);
    }

    @RequestMapping(value = "modifyPwd", method = RequestMethod.POST)
    @ResponseBody
    public Result modifyPwd(@RequestBody Map<String, Object> pwds){

        System.out.println(pwds);
        if(!pwds.isEmpty()){
            if(pwds.size() < 3){
                return new Result(false, "请输入完整内容",null);
            }
            String oldPwd = (String) pwds.get("oldPwd");
            String newPwd = (String) pwds.get("newPwd");
            String multiPwd = (String) pwds.get("multiPwd");
            if(!newPwd.equals(multiPwd)){
                return new Result(false, "新密码和重复密码不一致",null);
            }
            Subject subject = SecurityUtils.getSubject();
            String principal = (String) subject.getPrincipal();
            AdminInfo admin = service.findByAdmincode(principal);
            //验证成功
            if(admin.getAdminPassword().equals(EncryptUtils.MD5_HEX(admin.getAdminCode() + oldPwd))){
                admin.setAdminPassword(EncryptUtils.MD5_HEX(admin.getAdminCode() + newPwd));
                service.updateDetail(admin);
//                ShiroUtils.setAdmin(admin);
                return new Result(true, "更新成功", null);
            }else{
                return new Result(true, "旧密码不一致", null);
            }
        }else{
            return new Result(false, "请输入内容",null);
        }

    }

}
